package com.zizy.healthexpert.Activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.zizy.healthexpert.Adapter.listHistoryAdapter;
import com.zizy.healthexpert.Config.Config;
import com.zizy.healthexpert.Database.DatabaseHelper;
import com.zizy.healthexpert.Database.MyDatabase_PersonalBasicInf;
import com.zizy.healthexpert.R;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;


public class MainActivity extends Activity {
    public static MainActivity instance;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance=this;
        Config.Application.context=getApplicationContext();
        setContentView(R.layout.activity_main);

        //create database if not exist
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if(!DatabaseHelper.isDatabaseExist()){
                        new DatabaseHelper(MainActivity.this).createDatabase();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                           // Toast.makeText(getApplicationContext(), "資料庫建立完成", Toast.LENGTH_LONG).show();
                            /*Config*/
                            MyDatabase_PersonalBasicInf myDatabase_personalBasicInf =new MyDatabase_PersonalBasicInf(getBaseContext());
                            ArrayList<Object> list=myDatabase_personalBasicInf.SearchDatabase("*", String.format("ID=%s", Config.userID));
                            if ((list.size()!=0)){
                                MyDatabase_PersonalBasicInf.FieldsBasicInf perBasicInf=(MyDatabase_PersonalBasicInf.FieldsBasicInf) list.get(0);
                                Config.PersonalInf.age = perBasicInf.getAge();
                                Config.PersonalInf.gender=perBasicInf.getGender();
                                Config.PersonalInf.tag = perBasicInf.getLabour();
                            }
                        }
                    });




                } catch (IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "資料庫建立失敗，請重新安裝", Toast.LENGTH_LONG).show();

                        }
                    });
                    e.printStackTrace();
                }
            }
        }).start();


        /*test database*/
//        MyDatabase_Dishes dishesDatabase=new MyDatabase_Dishes(MainActivity.this);
//        MyDatabase_Dishes.FieldsDishes result=(MyDatabase_Dishes.FieldsDishes)dishesDatabase.SearchDatabase("*","dishesid=100").get(0);
//        Log.e("LOG",result.dishesName);
//        Toast.makeText(getApplicationContext(),result.dishesName,Toast.LENGTH_LONG).show();


        GridView gridview = (GridView) findViewById(R.id.gridView);
        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("ItemImage", R.drawable.mainactivity_record);//添加图像资源的ID record
        lstImageItem.add(map);

        map = new HashMap<String, Object>();
        map.put("ItemImage", R.drawable.mainactivity_search);//添加图像资源的ID
        lstImageItem.add(map);

        map = new HashMap<String, Object>();
        map.put("ItemImage", R.drawable.mainactivity_sports);//添加图像资源的ID
        lstImageItem.add(map);

        map = new HashMap<String, Object>();
        map.put("ItemImage", R.drawable.mainactivity_analyze);//添加图像资源的ID
        lstImageItem.add(map);

        map = new HashMap<String, Object>();
        map.put("ItemImage", R.drawable.mainactivity_center);//添加图像资源的ID
        lstImageItem.add(map);

        map = new HashMap<String, Object>();
        map.put("ItemImage", R.drawable.mainactivity_history);//添加图像资源的ID
        lstImageItem.add(map);

        map = new HashMap<String, Object>();
        map.put("ItemImage", R.drawable.mainactivity_setting_basic);//添加图像资源的ID
        lstImageItem.add(map);

        map = new HashMap<String, Object>();
        map.put("ItemImage", R.drawable.mainactivity_recommend);//添加图像资源的ID
        lstImageItem.add(map);

        map = new HashMap<String, Object>();
        map.put("ItemImage", R.drawable.mainactivity_setting_disease);//添加图像资源的ID
        lstImageItem.add(map);


        //生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
        SimpleAdapter saImageItems = new SimpleAdapter(this, //没什么解释
                lstImageItem,//数据来源
                R.layout.list_image_item,//night_item的XML实现
                //动态数组与ImageItem对应的子项
                new String[] {"ItemImage"},
                //ImageItem的XML文件里面的一个ImageView,两个TextView ID
                new int[] {R.id.listItemImage});
        //添加并且显示
        gridview.setAdapter(saImageItems);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String, Object> item=(HashMap<String, Object>) parent.getItemAtPosition(position);
                Intent intent=new Intent(MainActivity.this, RecordFoodsActivity.class);
                switch ((Integer)item.get("ItemImage")){
                    case  R.drawable.mainactivity_record:
                        int time=0;
                        switch (Calendar.getInstance().get(Calendar.HOUR_OF_DAY)/8){
                            case 1:
                                intent.putExtra("time", Config.BREAKFAST);
                                break;
                            case 2:
                                intent.putExtra("time", Config.LUNCH);
                                break;
                            case 3:
                                intent.putExtra("time", Config.DINNER);
                                break;
                            default:
                                intent.putExtra("time", Config.BREAKFAST);
                                break;
                        }
                        startActivity(intent);
//                        startActivity(new Intent (..this,NewRecordActivity.class));
                        break;
                    case  R.drawable.mainactivity_search:
                        SearchActivity.mode= Config.SEARCH;
                        startActivity(new Intent (MainActivity.this,SearchActivity.class));
                        break;
                    case  R.drawable.mainactivity_sports:
                        startActivity(new Intent (MainActivity.this,RecordSportsActivity.class));
                        break;
                    case  R.drawable.mainactivity_analyze:
                        startActivity(new Intent (MainActivity.this,AnalyzeActivity.class));
                        break;
                    case  R.drawable.mainactivity_center:
                        break;
                    case  R.drawable.mainactivity_history:
                        startActivity(new Intent (MainActivity.this,HistoryActivity.class));
                        break;
                    case  R.drawable.mainactivity_setting_basic:
                        startActivity(new Intent (MainActivity.this,SettingActivity.class));
                        break;
                    case  R.drawable.mainactivity_recommend:
                        Uri uri= Uri.parse("https://www.facebook.com/pages/%E5%81%A5%E5%BA%B7%E9%A3%B2%E9%A3%9F%E9%81%94%E4%BA%BA/559642124162772");
                        Intent i=new Intent(Intent.ACTION_VIEW,uri);
                        startActivity(i);

                        break;
                    case  R.drawable.mainactivity_setting_disease:
                        startActivity(new Intent(MainActivity.this,SettingDiseaseActivity.class));
                        break;

                }
            }
        });




    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //clicked the new record button
    public void btnNewRecordClicked(View v){
        startActivity(new Intent (MainActivity.this,NewRecordActivity.class));


    }
    //clicked the search button
    public void btnSearchClicked(View view){
        SearchActivity.mode= Config.SEARCH;
        startActivity(new Intent (MainActivity.this,SearchActivity.class));
    }


    //clicked the setting button
    public void btnSettingClicked(View v) {
        startActivity(new Intent(MainActivity.this, SettingActivity.class));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
